/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-12 10:40:26
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-16 08:45:01
 * @FilePath: \郭招娣\hou\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const Controller = require('egg').Controller;
class ClassListController extends Controller {
    // 查询全表
    async classLists() {
        const { ctx } = this;
        const list = await this.app.mysql.select('classLists');
        if (list) {
            ctx.body = {
                code: 200,
                msg: '获取成功',
                list
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '获取失败',
            }
        }
    }
    // 添加分类
    async AddClassLists() {
        let { ctx } = this;

        let { title, classValue } = ctx.request.body;
        console.log(title, classValue)
        const post = await this.app.mysql.get('classLists', { title });
        if (post) {
            ctx.body = {
                msg: '已存在',
                code: 404
            }
        } else {
            const list = await this.app.mysql.insert('classLists', { title, classValue });

            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '添加成功',
                    list: list
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '添加失败',
                }
            }
        }

    }

    // 删除分类
    async deleteClassLists(){
        const  { ctx }=this;
        const { id }=ctx.query;
        const result = await this.app.mysql.delete('classLists', { id });
        if(result){
            const list = await this.app.mysql.select('classLists');
            ctx.body={
                code:200,
                msg:'删除成功',
                list
            }
        }else{
            ctx.body = {
                code: 500,
                msg: '删除失败',
            }
        }
       
           

    }


}

module.exports = ClassListController;
